# -*- coding: utf-8 -*-
from common.service_config import get_server_id
from pyshare.utils.time_utils import getCT, isPassDay


class EmperorMixin(object):
    __slots__ = ()

    def asdict(self):
        return {
            'Hp': self.hp.asdict(),
            'Energy': self.energy.asdict(),
            'IsGetIdBindingAward': self.is_get_id_binding_award,
            'IsGetPhoneBindingAward': self.is_get_phone_binding_award,
            'IsGetMailBindingAward': self.is_get_mail_binding_award,
            'IsGetSocialBindingAward': self.is_get_social_binding_award,
            'WatchAdvertisingTimes': self.watch_advertising_times,
            'LastGetAwardTime': self.last_get_award_time,
            'IsGetAdvertisingAward': self.is_get_advertising_award,
            'LastCheckWatchedTime': self.last_check_watched_time,
            'LastWatchTime': self.last_watch_time,
            'EvaluationAwardStatus': self.evaluation_award_status,
            'ReplaceEquipmentTimes': self.replace_equipment_times,
            'LastReplaceEquipmentTime': self.last_replace_equipment_time,
            'ClothesColor': self.clothes_color,
        }

    def get_replace_equipment_times(self):
        """
        获取换每日换装备次数
        :return:
        """
        # 隔日刷新换装次数
        if isPassDay(self._record.LastReplaceEquipmentTime):
            self._record.ReplaceEquipmentTimes = 0
            self._record.LastReplaceEquipmentTime = getCT()
            self.save()
        return self._record.ReplaceEquipmentTimes

    def get_transform_equipment_times(self):
        """
        获取换每日装备转换次数
        :return:
        """
        # 隔日刷新装备转换次数
        if isPassDay(self._record.LastTransformEquipmentTime):
            self._record.TransformEquipmentTimes = 0
            self._record.LastTransformEquipmentTime = getCT()
            self.save()
        return self._record.TransformEquipmentTimes

    def update_role_cache(self, field, val):
        """
        刷新角色缓存信息
        :param field: RoleInfo.default_var内所拥有字段
        :param val: 修改的信息
        :return:
        """
        rid, server_id = '', 0
        try:
            rid, server_id = self.role_guid, get_server_id()  # 表里没有ser_id,所以取上下文， 原先也没有rid 需要确认是否有问题
            if not isinstance(server_id, int):
                return
            from pyshare.bll.utils.share.share_role_information import ShareRoleInformation
            role_cache = ShareRoleInformation.get_role_information_from_cache_db(self.role_guid, server_id)
            if role_cache:
                role_dict = role_cache._asdict()
                role_dict[field] = val
                ShareRoleInformation.set_role_information(self.role_guid, role_dict)
        except Exception as e:
            try:
                from pyshare.framework import runtime_logger as log
                from pyshare.utils.traceback_utils import print_exc
                log.warning('UpdateRoleCacheError: rid: {}, server_id: {}, field: {} \n {}'.
                            format(rid, server_id, field, print_exc(e)))
            except Exception as e:
                pass
